//
//  HMStatuesCellModel.swift
//  swift微博项目
//
//  Created by huodian on 16/9/27.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

class HMStatuesCellModel: NSObject {

    var vartarImage : UIImage?
    
    var memberImage : UIImage?
    
    var iconName :URL? 
    
    
    var  sourcFrom : String?
    
    
    var pictureInfo : [HMStatusPictureInfo]?{
        
        
        return  statues?.retweeted_status == nil ? statues?.pic_urls : statues?.retweeted_status?.pic_urls
        
    }
    
    
    var statues : HMStatuesModel?{
        didSet{
            
            realVartarImage()
            
            realIconName()
            realMemberImage()
            
            self.sourcFrom = dealTheSource()
            
            
        }
        
        
        
        
    }
    
    var creatTime:String?{
        //拿到后台给的时间数据
        let timeString = statues?.created_at ?? ""
        
        let dateFormatter = DateFormatter()
        
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        
        dateFormatter.locale = Locale(identifier: "en")
        
        let creatDate = dateFormatter.date(from: timeString)
        //如果日期转换不成功就返回空字符串
        guard let sinceDate = creatDate else {
            
            return ""
        }
        
        let calendarTime = Calendar.current
        //判断是否是今年
        if judgeIsYear(targetTime: sinceDate) {
            //判断是否是今天
            if calendarTime.isDateInToday(sinceDate)
            {
                
                let date  = Date()
                
                let time  = date.timeIntervalSince(sinceDate)
                
                if time < 60 {
                    
                    return "刚刚"
                    
                }else if time < 3600 {
                    
                    return "\(Int(time / 60))分钟之前"
                    
                }else
                {
                    return "\(Int(time/3600))小时之前"
                    
                }
        
            }else if calendarTime.isDateInYesterday(sinceDate)
            {
                
                dateFormatter.dateFormat = "昨天 HH:mm"
                
                return dateFormatter.string(from: sinceDate)
                
            }else
            {
                dateFormatter.dateFormat = "MM-dd"
                return dateFormatter.string(from: sinceDate)
            }
            
        }else
        {
            dateFormatter.dateFormat = "yyyy-MM-dd"
            
            return dateFormatter.string(from: sinceDate)
            
        }
        
    }
    
    
    func judgeIsYear(targetTime:Date) -> Bool{
        
        let dateFomatter = DateFormatter()
        
        dateFomatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        //如果不设置此项,在适配的时候回出现问题
        dateFomatter.locale = Locale(identifier: "en")
        
        let date = Date()
        
        dateFomatter.dateFormat = "yyyy"
        
        let currentTime = dateFomatter.string(from: date)
        
        let creatTime = dateFomatter.string(from: targetTime)
        
        return  currentTime == creatTime
        
        
        
        
        
        
    }
    //处理微博来源(字符串的截取)
    func dealTheSource()->String{
//"<a href=\"http://app.weibo.com/t/feed/4r5Qx7\" rel=\"nofollow\">微博视频</a>"
        let sourceStr = statues?.source ?? ""
        
        
        let starFlag = "\">"
        
        let endFlag = "</a>"
        
        let starRang = sourceStr.range(of: starFlag)
        
        let endRang = sourceStr.range(of: endFlag)
        guard let starIndex = starRang?.upperBound , let endIndex = endRang?.lowerBound else {
            
            return "来自氪星"
        }
      
        
        let indexRang1 = starIndex..<endIndex
        
        let sourceName = sourceStr.substring(with: indexRang1)
        
        
        
        return sourceName
        
        
        
        
        
        
    }
    
    func realIconName(){
        
        let str  = statues?.user?.avatar_large ?? ""
        
        iconName = URL(string: str)
        
        
        
        
    }
    
    func realVartarImage()
    {
        let  viertify = statues?.user?.verified_type ?? -1
        
        
        
        switch viertify {
        case 0:
            vartarImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
              vartarImage = #imageLiteral(resourceName: "avatar_grassroot")
        case 220:
            vartarImage = #imageLiteral(resourceName: "avatar_vgirl")
        default :
            vartarImage = nil
         
        }
        
        
    }
   func realMemberImage()
   {
    
    if let  mbrank = statues?.user?.mbrank, mbrank > 0 && mbrank < 7
    {
        let str = "common_icon_membership_level\(mbrank)"
        
        
        memberImage = UIImage(named: str)
        
      
        
    }
    
    
    }
    
}
